<cfcomponent output="false">

	<!--- --->

	<cffunction name="getWorkweekEvents" access="public" output="true" returntype="query">
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		<cfargument name="workweekEventID" required="false" />
		<cfargument name="goalActionID" required="false" />
		<cfargument name="weekdays" required="false" />
		<cfargument name="scheduled" required="false" />
		<cfargument name="noFuture" required="false" default="false" hint="for future goals" />
		<cfargument name="goalAbandoned" required="false" />
		<cfargument name="active" required="false" default="1" />
		<cfargument name="modified_after" required="false" default="" />
		
		<cfset var local = {} />

		<cfquery name="local.getWorkweekEvents" datasource="#$.site.getVar('datasource')#">
			select 
			hex(workweek_event.id) as id,
			hex(workweek_event.goal_action_id) as goal_action_id,
			hex(goal.id) as goal_id,
			goal.name as goal_name,
			workweek_event.name,
			date_format(workweek_event.start_time,'%H:%i:%s') as start_time,
			hour(workweek_event.start_time) as hour,
			minute(workweek_event.start_time) as minute,
			workweek_event.duration,
			workweek_event.weekday,
			workweek_event.created,
			ifNull(workweek_event.category,0) as category,
			workweek_event.active
			from workweek_event
			left join goal_action on goal_action.id = workweek_event.goal_action_id
				and goal_action.active = 1
			left join goal on goal.id = goal_action.goal_Id
				and goal.active = 1
			where 1=1
			<cfif arguments.active neq "">
				and workweek_event.active = #$.database.string(arguments.active)#
			</cfif>
			and workweek_event.user_id = #$.database.uuid(arguments.userID)#
			<cfif structKeyExists(arguments,"workweekEventID")>
				and workweek_event.id = #$.database.uuid(arguments.workweekEventID)#
			</cfif>
			<cfif structKeyExists(arguments,"goalActionID")>
				and workweek_event.goal_action_id = #$.database.uuid(arguments.goalActionID)#
			</cfif>
			<cfif structKeyExists(arguments,"weekdays")>
				and workweek_event.weekday in #$.database.strings(arguments.weekdays)#
			</cfif>
			<cfif structKeyExists(arguments,"scheduled")>
				and workweek_event.start_time is <cfif arguments.scheduled eq true>not</cfif> null
			</cfif>
			<cfif arguments.noFuture>
				and (goal.start < '#dateTimeFormat(now(),"yyyy-mm-dd HH:nn:ss")#' or goal.id is null)
			</cfif>
			<cfif structKeyExists(arguments,"goalAbandoned")>
				and (workweek_event.goal_action_id is null
					or goal.abandoned = #$.database.string(arguments.goalAbandoned)#)
			</cfif>
			<cfif arguments.modified_after neq "">
				and workweek_event.modified >= #$.database.datetime(arguments.modified_after)#
			</cfif>
			order by workweek_event.weekday, workweek_event.start_time asc
		</cfquery>

		<cfreturn local.getWorkweekEvents />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="bulkDeleteWorkweekEvents" access="public" output="true" returntype="void">
		<cfargument name="goalID" required="false" default="" />
		<cfargument name="actionID" required="false"default="" />
		
		<cfset var local = {} />

		<cfif arguments.goalID neq "" or arguments.actionID neq "">
			<cfquery name="local.bulkDeleteActions" datasource="#$.site.getVar('datasource')#">
				update workweek_event
				left join goal_action on goal_action.id = workweek_event.goal_action_id
				set workweek_event.active = 0,
					workweek_event.modified = now()
				where workweek_event.active = 1
				<cfif arguments.actionID neq "">
					and workweek_event.goal_action_id = #$.database.uuid(arguments.actionID)#
				</cfif>
				<cfif arguments.goalID neq "">
					and goal_action.goal_Id = #$.database.uuid(arguments.goalID)#
				</cfif>
			</cfquery>
		</cfif>
		
	</cffunction>
	
	<!--- --->

</cfcomponent>